package webshop.data;

import java.util.Properties;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;



/**
 * A class that creates data in the database for testing.
 * @author even & simon.
 */
public class Populator {

    public Populator() {
        Properties props = new Properties();
        props.put("toplink.ddl-generation", "drop-and-create-tables");
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("webshop", props);

        //create the tables
        emf.createEntityManager().close();
        emf.close();
    }

    // Populates the database with tests data.
    public void populate() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("webshop");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();

        Person per1 = new Person("Simon Zimmermann", "Lømslandsveien", 41587670);
        em.persist(per1);

        Person per2 = new Person("Renathe Todal Tande", "Kristiansand", 9568264);
        em.persist(per2);

        Person per3 = new Person("Vegard Andersen", "Tveit", 22222222);
        em.persist(per3);

        Person per4 = new Person("Sindre Ohm", "Lund", 1111111);
        em.persist(per4);

        // Products
        Product pro1 = new Product("Dagbladet", "Hver dag det samme.", 20, 100);
        em.persist(pro1);

        Product pro2 = new Product("Morgenbladet", "Ukesavis. Akademianytt.", 29, 100);
        em.persist(pro2);

        Product pro3 = new Product("Verdens Gang", "Blablabla. Ikke noe nytt", 15, 100);
        em.persist(pro3);

        Product pro4 = new Product("Federlandsvennen", "Kristiansand kristen avis", 15, 100);
        em.persist(pro4);

        em.getTransaction().commit();
        em.close();
        emf.close();
    }


    public static void main(String[] args) {
        System.getProperties().list(System.out);
        Populator m = new Populator();
        m.populate();
    }

}